//
//  GEPickerTextField.m
//  OMS
//
//  Created by goldeney on 14/10/23.
//  Copyright (c) 2014年 ShangHai Goldeneye Techonology Co.,Ltd. All rights reserved.
//

#import "GEPickerTextField.h"
#import "NSDictionary+DataFormat.h"
#import "IQUIView+IQKeyboardToolbar.h"
#import "IQUIView+Hierarchy.h"

@interface GEPickerTextField ()<UIPickerViewDelegate,UIPickerViewDataSource>

@property (strong, nonatomic) UIPickerView *customPicker;
@property (strong, nonatomic) UIView *keyboardView;

@property (strong, nonatomic) NSArray *fontAry;
@property (strong, nonatomic) NSArray *keyValue;

@property (strong, nonatomic) id target;
@property (assign, nonatomic) SEL action;

@end

@implementation GEPickerTextField
@synthesize fontAry;
@synthesize selectData;

- (id)initWithCoder:(NSCoder *)aDecoder
{
    self = [super initWithCoder:aDecoder];
    if (self) {
        [self initWithTextField];
    }
    return self;
}

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self initWithTextField];
    }
    return self;
}

- (void)initWithTextField
{
    self.keyboardView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SuperWidth, 162)];
    self.inputView = self.keyboardView;
    
    self.customPicker = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 0, SuperWidth, 162)];
    self.customPicker.delegate = self;
    self.customPicker.dataSource = self;
    [self.keyboardView addSubview:self.customPicker];
    
    //    [self addCancelDoneOnKeyboardWithTarget:self cancelAction:@selector(cancelAction:) doneAction:@selector(doneAction:) titleText:self.placeholder];
    [self addPreviousNextDoneOnKeyboardWithTarget:self previousAction:@selector(previousAction:) nextAction:@selector(nextAction:) doneAction:@selector(doneAction:) titleText:self.placeholder];
}

- (void)setInputViewNull
{
    self.inputView = nil;
    [self addPreviousNextDoneOnKeyboardWithTarget:self previousAction:@selector(previousAction:) nextAction:@selector(nextAction:) doneAction:@selector(doneNotEventAction:) titleText:self.placeholder];
}

- (void)setInputViewPicker
{
    self.inputView = self.keyboardView;
    [self addPreviousNextDoneOnKeyboardWithTarget:self previousAction:@selector(previousAction:) nextAction:@selector(nextAction:) doneAction:@selector(doneAction:) titleText:self.placeholder];
}

- (BOOL)textViewShouldBeginEditing:(UITextView *)textView{
    return NO;
}

- (NSMutableString*)stringGenerate:(NSDictionary*)dictionary
{
    NSMutableString *string = [NSMutableString string];
    for (int i=0;i<self.keyValue.count;i++){
        NSDictionary *keyData = [self.keyValue objectAtIndex:i];
        NSString *key = keyData.allKeys.firstObject;
        NSString *value = [keyData objectFormatForKey:key];
        NSString *keyString = [dictionary objectFormatForKey:key];
        //数据不存在及不显示
        if (keyString) {
            NSString *dataString = [NSString stringWithFormat:value,keyString];
            [string appendString:dataString];
        }
    }
    
    //未有任何数据
    if (string.length<=0) {
        [string appendString:@""];
    }
    
    return string;
}

- (void)addDoneTarget:(id)target action:(SEL)action
{
    self.target = target;
    self.action = action;
}

-(void)doneAction:(UIBarButtonItem*)barButton
{
    //在输入状态直接关闭
    if (self.keyboardIsInput && self.inputView == nil) {
        self.inputView = self.keyboardView;
//        [self addCancelDoneOnKeyboardWithTarget:self cancelAction:@selector(cancelAction:) doneAction:@selector(doneAction:) titleText:self.placeholder cancelTitle:@"键盘输入"];
        [self addCancelDoneOnKeyboardWithTarget:self cancelAction:@selector(cancelAction:) doneAction:@selector(doneAction:) titleText:self.placeholder];
        
    } else {
        if (self.selectData) {//选择了数据
            NSMutableString *string = [self stringGenerate:self.selectData];
            self.text = string;
        } else if (fontAry && fontAry.count>0) {//未选中数据
            self.selectData = [fontAry firstObject];
            NSMutableString *string = [self stringGenerate:self.selectData];
            self.text = string;
        }
        
        if (self.target && self.action)
        [self.target performSelector:self.action withObject:nil];
    }
    
    //关闭键盘
    [self resignFirstResponder];
}

-(void)cancelAction:(UIBarButtonItem*)barButton
{
    if (!self.keyboardIsInput) {
        [self resignFirstResponder];
    } else {
        [self setKeyboard:barButton];
    }
    
}

-(void)doneNotEventAction:(UIBarButtonItem*)barButton
{
    [self resignFirstResponder];
}

/*! previousAction. */
-(void)previousAction:(id)button
{
    //previousAction
    NSArray *responder =[self responderSiblings];
    NSInteger index = [responder indexOfObject:self];
    if (index-1>=0) {
        UITextField *textField = [responder objectAtIndex:index-1];
        [textField becomeFirstResponder];
        [self doneAction:nil];
    }
    
}

/*! nextAction. */
-(void)nextAction:(id)button
{
    NSArray *responder =[self responderSiblings];
    NSInteger index = [responder indexOfObject:self];
    if (index<responder.count) {
        UITextField *textField = [responder objectAtIndex:index+1];
        [textField becomeFirstResponder];
        [self doneAction:nil];
    }
}

- (void)setKeyboard:(UIBarButtonItem*)barButton
{
    if(self.inputView == nil){
        self.inputView = self.keyboardView;
        [self resignFirstResponder];
        [self becomeFirstResponder];
//        [self addCancelDoneOnKeyboardWithTarget:self cancelAction:@selector(cancelAction:) doneAction:@selector(doneAction:) titleText:self.placeholder cancelTitle:@"键盘输入"];
        [self addCancelDoneOnKeyboardWithTarget:self cancelAction:@selector(cancelAction:) doneAction:@selector(doneAction:) titleText:self.placeholder];
        
    } else {
        self.inputView = nil;
        [self resignFirstResponder];
        [self becomeFirstResponder];
//        [self addCancelDoneOnKeyboardWithTarget:self cancelAction:@selector(cancelAction:) doneAction:@selector(doneAction:) titleText:self.placeholder cancelTitle:@"对象选择"];
        [self addCancelDoneOnKeyboardWithTarget:self cancelAction:@selector(cancelAction:) doneAction:@selector(doneAction:) titleText:self.placeholder];
    }
}

- (void)setISInput:(BOOL)isInput
{
    self.keyboardIsInput = isInput;
    if (!self.keyboardIsInput) {
        [self addPreviousNextDoneOnKeyboardWithTarget:self previousAction:@selector(previousAction:) nextAction:@selector(nextAction:) doneAction:@selector(doneAction:) titleText:self.placeholder];
    } else {
//        [self addCancelDoneOnKeyboardWithTarget:self cancelAction:@selector(cancelAction:) doneAction:@selector(doneAction:) titleText:self.placeholder cancelTitle:@"切换输入"];
        [self addCancelDoneOnKeyboardWithTarget:self cancelAction:@selector(cancelAction:) doneAction:@selector(doneAction:) titleText:self.placeholder];
    }
}

- (void)setComponents:(NSArray*)array keyValue:(NSArray*)keyValue
{
    if (array && keyValue) {
        self.keyValue = keyValue;
        self.fontAry = array;
        [self.customPicker reloadAllComponents];
    }
}

#pragma mark pickerview function

/* return cor of pickerview*/
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    return 1;
}

/*return row number*/
-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    return [fontAry count];
}

/*return component row str*/
-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
    if (row < fontAry.count && row >= 0) {
        NSDictionary *data = [fontAry objectAtIndex:row];
        return [self stringGenerate:data];
    } else {
        return @"";
    }
}

/*choose com is component,row's function*/
-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
    // NSLog(@"font %@ is selected.",row);
    if (row < fontAry.count && row >= 0) {
        NSDictionary *data = [fontAry objectAtIndex:row];
        self.selectData = data;
    }
}

@end
